local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_item_frozen_npc"] or {}
end



local function fn()
    local inst = CreateEntity()
    inst.entity:AddTransform()
    
    inst:AddTag("nosteal")
	inst:AddTag("irreplaceable")        ---- 不给带出当前世界

    if not TheWorld.ismastersim then
        return inst
    end


    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")

    inst:DoTaskInTime(0,function()
        if TUNING.NPC_CONFIG.CAVE_ICE_SCULPTURE ~= true then
            inst:Remove()
            print("info config : CAVE_ICE_SCULPTURE false")
            return
        end
        local npc_name = inst.npc_everything_data:Get("npc_prefab") or inst.npc_base_lib:GetRandom(TUNING.NPC_CONFIG.NPC_NAME_LIST).."_npc"
        print("info npc event frozen npc name :",npc_name)
        inst.npc_everything_data:Set("npc_prefab",npc_name)
        -- print("info 55555555555")

        local npc = SpawnPrefab(npc_name) or SpawnPrefab("wilson_npc")
        -- print("info ： ",npc.IsInAnyStormOrCloud)
        RemovePhysicsColliders(npc)
        inst.npc = npc
        npc.Transform:SetPosition(inst.Transform:GetWorldPosition())
        npc.Ready = true
        npc.AnimState:OverrideSymbol("swap_frozen", "frozen", "frozen")
        npc.AnimState:PlayAnimation("frozen")
        npc:ClearStateGraph()

        npc:RemoveComponent("health")
        npc:RemoveComponent("sanity")
        npc:RemoveComponent("hunger")
        npc:RemoveComponent("combat")

        npc:AddComponent("workable")
        npc.components.workable:SetWorkAction(ACTIONS.MINE)
        npc.components.workable:SetWorkLeft(inst.npc_everything_data:Get("work_left") or (TUNING.NPC_MOD_TEST_DEBUG and 10) or 200)
        npc.components.workable:SetOnWorkCallback(function()
            inst:PushEvent("action_work")
            npc.Transform:SetPosition(inst.Transform:GetWorldPosition())
            inst.npc_everything_data:Set("work_left",npc.components.workable:GetWorkLeft())
        end)
        npc.components.workable:SetOnFinishCallback(function()
            npc.Transform:SetPosition(inst.Transform:GetWorldPosition())
            npc:AddTag("INLIMBO")
            npc.AnimState:PlayAnimation("frozen_loop_pst", true)
            npc.SoundEmitter:PlaySound("dontstarve/common/freezethaw", "thawing")
            npc:DoTaskInTime(3, function()
                    npc.SoundEmitter:KillSound("thawing")
                    npc.AnimState:ClearOverrideSymbol("swap_frozen")
                    npc.AnimState:PlayAnimation("hit")
                    local fx = SpawnPrefab("shatter")
                    fx.Transform:SetPosition(npc.Transform:GetWorldPosition())
                    fx.components.shatterfx:SetLevel(4)
                    npc:ListenForEvent("animover",function()
                        inst:PushEvent("work_finish")                    
                    end)
                
            end)
        end)

        npc:DoTaskInTime(1, function()
            npc.components.inspectable:SetDescription(GetStringsTable().inspect_str)
            local name = GetStringsTable().name .. npc.components.named.name
            npc.components.named:SetName(name)
        end)

    end)

    inst:ListenForEvent("action_work",function()
        if math.random(100) <= 30 or TUNING.NPC_MOD_TEST_DEBUG then
            inst.npc_base_lib:Throw_Out_Items({
                    target = inst,
                    name = "ice",
                    num = 1,    
                    range = 5,
                    height = 4,
                    random_height = true
            })
        end
    end)

    inst:ListenForEvent("work_finish",function()
        if inst.npc and inst.npc.npc_base_lib then
            inst.npc:Remove()
            inst.npc.npc_base_lib:RPC_PushEvent("RPC_Remove")
        end
        local npc_name = inst.npc_everything_data:Get("npc_prefab")
        local player = inst:GetNearestPlayer()
        if player and player:HasTag("npc_tag.isplayer") then
            local npc = SpawnPrefab(npc_name)
            npc.Transform:SetPosition(inst.Transform:GetWorldPosition())
            npc:PushEvent("player_link2",player)
            npc:AddDebuff("npc_event_frozen_gift_task", "npc_event_frozen_gift_task")
        end
        inst:Remove()
    end)


    return inst
end




return Prefab("npc_item_frozen_npc", fn)